

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorMain {

    private static final String PREJUDICATION = "config/generatorConfig-prejudication.xml";
    private static final String SPIDER = "config/generatorConfig-spider.xml";
    private static final String GYDATA = "config/generatorConfig-gydata.xml";
    private static final String GYDATA_BM = "config/generatorConfig-gydata-bm.xml";
    private static final String DATA_CENTER = "config/generatorConfig-zhdata.xml";
    private static final String DMP = "config/generatorConfig-dmp.xml";
    private static final String XZ_CRM = "config/generatorConfig-xzcrm.xml";
    private static final String KM_DATA = "config/generatorConfig-kmdata.xml";
    private static final String GY_DAPG = "config/generatorConfig-gydapg.xml";
    private static final String ZH_BOSS = "config/generatorConfig-zhboss.xml";
    private static final String ADB = "config/generatorConfig-adb.xml";

    private static final String CONF_FILE_NAME = ZH_BOSS;

    public static void main(String[] args) throws Exception {
        deleteExistFiles();
        GeneratorMain generatorMain = new GeneratorMain();
        generatorMain.generator(args);
    }


    public void generator(String[] args) throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);

        File configFile = new File(CONF_FILE_NAME);
        Configuration config = cp.parseConfiguration(configFile);
        readContextsProperties(config);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }

    private static void readContextsProperties(Configuration config) {
        for (Context context : config.getContexts()) {
            JavaModelGeneratorConfiguration javaModelConf = context.getJavaModelGeneratorConfiguration();
            SqlMapGeneratorConfiguration sqlMapConf = context.getSqlMapGeneratorConfiguration();
            JavaClientGeneratorConfiguration javaClientConf = context.getJavaClientGeneratorConfiguration();
            readProperties(context, javaModelConf, sqlMapConf, javaClientConf);
        }
    }

    /**
     * 读取定义的变量
     *
     * @param context
     * @param targetPackageConfs
     */
    public static void readProperties(Context context, TargetPackageConf... targetPackageConfs) {
        for (TargetPackageConf conf : targetPackageConfs) {
            String targetPackage = conf.getTargetPackage();
            Pattern pattern = Pattern.compile("\\$\\{([\\w.]*)}");
            Matcher matcher = pattern.matcher(targetPackage);
            if (matcher.find()) {
                String varName = matcher.group(1);
                String property = context.getProperty(varName);
                if (property == null) throw new RuntimeException(String.format("未找到变量:%s", varName));
                conf.setTargetPackage(property);
            }
        }

    }

    private static void deleteExistFiles() {
        File javaFileDir = new File("src/com/zh");
        deleteDir(javaFileDir);
        File mapperDir = new File("src/config/mapper");
        deleteDir(mapperDir);
    }

    private static void deleteDir(File file) {
        if (!file.exists()) return;
        if (file.isFile()) {
            //noinspection ResultOfMethodCallIgnored
            file.delete();
        } else if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null || files.length == 0) return;
            for (File file1 : files) {
                deleteDir(file1);
            }
        }
    }
}
